package com.yantong.controller.api;

import com.yantong.annotation.Auth;
import com.yantong.constant.BusinessCode;
import com.yantong.constant.CentreType;
import com.yantong.constant.PayStatus;
import com.yantong.controller.BaseController;
import com.yantong.exception.BDException;
import com.yantong.pojo.Centre;
import com.yantong.pojo.ListOrderParams;
import com.yantong.pojo.Member;
import com.yantong.pojo.ProjectExp;
import com.yantong.pojo.vo.CentreVo;
import com.yantong.pojo.vo.ProjectExpVo;
import com.yantong.result.ResultVo;
import com.yantong.service.CentreService;
import com.yantong.service.MemberService;
import com.yantong.service.OrderService;
import com.yantong.service.ProjectExpService;
import com.yantong.utils.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author: juzi
 * @date: 2021/10/25
 * @time: 16:12
 */
@ApiIgnore
@Controller
@RequestMapping("/wxapi/center")
public class ApiCenterController extends BaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CentreService centreService;
    @Autowired
    private ProjectExpService projectExpService;
    @Autowired
    private MemberService memberService;




    /**
     * 商户列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultVo getCenters(Long mid) {
        List<Centre> centres = new ArrayList<Centre>();
        if (mid == null || mid == 0) {
            centres = centreService.listAllCentres();
        } else {
            //商户用户
            Centre centre = centreService.selectByMid(mid);
            centres.add(centre);
        }
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", centres);
        return resultVo;
    }

    /**
     * 用户商户账户信息
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultVo getCenterInfo(Long mid) {
        if (mid == null) {
            ResultVo.error();
        }
        Centre centre = centreService.getBymid(mid);
        if (centre == null) {
            ResultVo resultVo = new ResultVo();
            resultVo.put("success", true);
            resultVo.put("data", centre);
            return resultVo;
        }
        ProjectExpVo params = new ProjectExpVo();
        params.setType(0);
        params.setId(centre.getProjectexpId());
        ProjectExp projectExp = projectExpService.listProjectExp(params);
        if (projectExp != null) {
            centre.setChargeType(projectExp.getChargeType());
        } else {
            centre.setChargeType(0);
        }

        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", centre);
        return resultVo;
    }

    /**
     * 用户商户账户信息
     */
    @RequestMapping("/infoByCenterId")
    @ResponseBody
    public ResultVo getInfoByCenterId(Long centerId) {
        if (centerId == null) {
            return ResultVo.error();
        }
        Centre centre = centreService.getByKey(centerId);
        if (centre == null) {
            return ResultVo.error();
        }
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", centre);
        return resultVo;
    }

    /**
     * 用户商户账户信息
     */
    @RequestMapping("/infoByTeamSeqId")
    @ResponseBody
    public ResultVo getInfoByTeamSeqId(String teamSeqId) {
        if (ValidateUtil.isEmptyString(teamSeqId)) {
            return ResultVo.error();
        }
        Member teamMember = memberService.getTmByTeamSeqId(teamSeqId);
        if (teamMember == null) {
            throw new BDException("门店信息错误，请联系系统管理员");
        }
        Centre centre = centreService.getByKey(teamMember.getCentreId());
        if (centre == null) {
            return ResultVo.error();
        }
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", centre);
        return resultVo;
    }

    /**
     * 用户商户流水
     */
    @RequestMapping("/amountLogs")
    @ResponseBody
    public ResultVo getListAmountLogs(Long mid, Date start, Date end, String orderSn, Integer flowType, int offset, int limit) {
        if (mid == null) {
            ResultVo.error();
        }
        Centre centre = centreService.selectByMid(mid);
        if (centre == null) {
            ResultVo.error();
        }
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", centreService.getListAmountLogs(centre.getCentreId(), start, end, orderSn, flowType, offset, limit));
        return resultVo;
    }

    /**
     * 保存站点
     *
     * @param referrals 推荐人id
     * @return
     */
    @RequestMapping("/centre/save")
    @ResponseBody
    @Auth
    public ResultVo saveCentre(CentreVo centre, String areas, Long referrals) {
        if (centre == null) {
            return ResultVo.error();
        }
        //商户用户
        Centre centreMember = centreService.selectByMid(centre.getMid());
        if (centreMember != null) {
            ResultVo resultVo = new ResultVo();
            resultVo.put("success", true);
            resultVo.put("data", centreMember);
            return resultVo;
        }
        //商户购买区域城市
        centre.setSaleAreas(centre.getCity());
        if (CentreType.COMPANY.getCode().equals(centre.getType())) {
            /**公司需校验营业执照等**/
            if (StringUtils.isEmpty(centre.getBusiLicense())) {
                return ResultVo.error(BusinessCode.BUSINESS_LICENSE_NULL);
            }
            if (StringUtils.isEmpty(centre.getBusiCategory())) {
                return ResultVo.error(BusinessCode.BUSINESS_CATEGORY_NULL);
            }
            if (StringUtils.isEmpty(centre.getRegistCapital())) {
                return ResultVo.error(BusinessCode.REGIST_CAPITAL_NULL);
            }
            if (centre.getRegistDate() == null) {
                return ResultVo.error(BusinessCode.REGIST_DATE_NULL);
            }
        }
        //设置为待审核
        centre.setStatus("2");
        //商品设置为审核
        centre.setVerify(1);
        if (centre.getProjectexpId() == null) {
            centre.setProjectexpId(6L);
            centre.setPayStatus(PayStatus.PAY_SUCCESS.getCode());
//			centre.setRenewMid(centre.getMid());

            Calendar curr = Calendar.getInstance();
            curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 100);
            Date date = curr.getTime();
            centre.setExpiredDate(date);
        }
        centre.setRenewMid(centre.getMid());
        if (centreService.saveCentre(centre) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 得到用户商户信息
     *
     * @return
     */
    @RequestMapping("/centre/getInfo")
    @ResponseBody
    @Auth
    public ResultVo getInfo(Long mid) {
        ResultVo resultVo = new ResultVo();
        //商户用户
        Centre centre = centreService.selectByMid(mid);

        if (centre != null && centre.getStatus().equals("2") && centre.getPayStatus() != 2) {
            ListOrderParams listOrderParams = new ListOrderParams();
            listOrderParams.setStatus(0);
            listOrderParams.setMid(mid);
            listOrderParams.setIsCentreord(1);
            Long orderId = orderService.getWaitOrderId(listOrderParams);
            if (orderId != null) {
                centre.setOrderId(orderId);
            }
        }

        resultVo.put("success", true);
        resultVo.put("data", centre);
        return resultVo;
    }



}
